package com.sino.operation.util;

import com.adtec.ms.msagent.service.AddressingService;
import com.sino.operation.Exception.sinoexception.CoreUrlException;
import com.sino.operation.commons.response.CenterResponseEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName UrlUtil
 * @Description 寻址
 * @Author 北陈
 * @Date 2021/10/25 9:35
 * @Version 1.0
 */
@Component
public class UrlUtil {

    @Autowired
    private StringRedisTemplate redis;

    @Autowired(required = false)
    private AddressingService addressingService;

    public String getCoreUrl(String interNo){
        String escToken = redis.opsForValue().get("escToken");
        String seqNo= UUID.randomUUID().toString();
        HashMap<String, Object> svrAddr = addressingService.getSvrAddr(seqNo, escToken, "sxyh", "xy0001", interNo, null);
        Map com_http = (Map) svrAddr.get("COM_HTTP");
        String url=(String)com_http.get("URL");
        if (StringUtils.isEmpty(url)||StringUtils.isBlank(url)){
            throw new CoreUrlException(CenterResponseEnum.CORE_URL_ERROR);
        }
        return url;
    }

    //获取账管地址
    public String getAccountManage(String interNo){
        String escToken = redis.opsForValue().get("escToken");
        String seqNo= UUID.randomUUID().toString();
        HashMap<String, Object> svrAddr = addressingService.getSvrAddr(seqNo, escToken, "sxyh", "2260000", interNo, null);
        Map com_http = (Map) svrAddr.get("COM_HTTP");
        String url=(String)com_http.get("URL");
        if (StringUtils.isEmpty(url)||StringUtils.isBlank(url)){
            throw new CoreUrlException(CenterResponseEnum.CORE_URL_ERROR);
        }
        return url;
    }
    public String getEDUrl(String interNo){
        String escToken = redis.opsForValue().get("escToken");
        String seqNo= UUID.randomUUID().toString();
        HashMap<String, Object> svrAddr = addressingService.getSvrAddr(seqNo, escToken, "sxyh", "ag0001", interNo, null);
        Map com_http = (Map) svrAddr.get("COM_HTTP");
        String url=(String)com_http.get("URL");
        return url;
    }

    public String getCounterUrl(String interNo){
        String escToken = redis.opsForValue().get("escToken");
        String seqNo= UUID.randomUUID().toString();
        HashMap<String, Object> svrAddr = addressingService.getSvrAddr(seqNo, escToken, "sxyh", "ag0002", interNo, null);
        Map com_http = (Map) svrAddr.get("COM_HTTP");
        String url=(String)com_http.get("URL");
        return url;
    }

}
